package org.gruposp2p.proyectokarma.controller

import org.gruposp2p.cometd.KarmaMessageService;
import org.gruposp2p.cometd.KarmaMessageEvent;
import org.gruposp2p.proyectokarma.domain.CometdMessage;

class MessageServiceController {

    KarmaMessageService karmaMessageService;
	def eventBroker;

    def index = { }

    def sendMessage = {
        def result = karmaMessageService.sendMessageToPeer(params.message, params.channel, params.peerId);
        render "Result: " + result + " - Message: " + params.message + " - channel: " + params.channel  
    }

	def publishAlert = {
		CometdMessage cometdMessage = new CometdMessage(area:params.area ,content:params.message,
			channel:KarmaMessageEvent.ALERT_CHANNEL, date:new Date())
		eventBroker.publish(KarmaMessageEvent.ALERT_CHANNEL, new KarmaMessageEvent(cometdMessage));
		render "Alert published"
	}
	
    def getChannels = { }
    
}